package com.gt.self.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.common.AjaxResult;
import com.gt.self.domain.UsMachineList;
import com.gt.self.domain.UsPrintModelConfig;
import com.gt.self.domain.UsPrintSet;
import com.gt.self.domain.UsSelfConfiguration;
import com.gt.self.domain.vo.SelfConfigurationVo;
import com.gt.self.service.UsMachineListService;
import com.gt.self.service.UsPrintModelConfigService;
import com.gt.self.service.UsPrintSetService;
import com.gt.self.service.UsSelfConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.print.PrintService;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "自助机配置")
@RestController
@RequestMapping("self/configuration")
//@CrossOrigin
public class SelfConfigurationController {

    @Autowired
    private UsSelfConfigurationService usSelfConfigurationService;

    @Autowired
    private UsPrintModelConfigService usPrintModelConfigService;

    @Autowired
    private UsMachineListService usMachineListService;

    @Autowired
    private UsPrintSetService usPrintSetService;


    @ApiOperation("保存打印配置")
    @PostMapping("saveSelfConfiguration")
    public AjaxResult saveSelfConfiguration(@RequestBody List<UsPrintModelConfig> usPrintModelConfigs) {

        if (usPrintModelConfigs == null && usPrintModelConfigs.size() < 0) {
            return AjaxResult.error("保存失败");
        }
        LambdaQueryWrapper<UsPrintModelConfig> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UsPrintModelConfig> lambdaQueryWrapper = wrapper.gt(UsPrintModelConfig::getId, 0);

        usPrintModelConfigService.remove(lambdaQueryWrapper);

        for (UsPrintModelConfig usPrintModelConfig : usPrintModelConfigs) {
            usPrintModelConfigService.save(usPrintModelConfig);
        }
        return AjaxResult.success();
    }


    @ApiOperation("获取打印配置")
    @GetMapping("selectSelfConfiguration")
    public AjaxResult selectSelfConfiguration() {
        List<UsPrintModelConfig> usPrintModelConfigs = usPrintModelConfigService.list();
        return AjaxResult.success().put("usPrintModelConfigs", usPrintModelConfigs);
    }

    @ApiOperation("查询打印字段")
    @GetMapping("selectPrintField")
    public AjaxResult selectPrintField() {

        List<String> printField = usSelfConfigurationService.selectPringField();
        return AjaxResult.success(printField);
    }

    @ApiOperation("获取是否自助打印标志")
    @GetMapping("getIsPrint")
    public AjaxResult getIsPrint() {
        SelfConfigurationVo selfConfigurationVo = new SelfConfigurationVo();

        List<UsSelfConfiguration> usSelfConfigurations = usSelfConfigurationService.list();
        for (UsSelfConfiguration selfConfiguration : usSelfConfigurations) {
            if (StringUtils.isNotBlank(selfConfiguration.getDescribe()) &&selfConfiguration.getDescribe().equals("自助机进入方式")){
                selfConfigurationVo.setMachineId(selfConfiguration.getId());
                selfConfigurationVo.setEntryType(selfConfiguration.getEntryType());
                selfConfigurationVo.setMachineFlag(selfConfiguration.getMachineFlag());
            }
            if (StringUtils.isNotBlank(selfConfiguration.getDescribe()) &&selfConfiguration.getDescribe().equals("是否打印标记")){
                selfConfigurationVo.setIsRepeatPrint(selfConfiguration.getIsRepeatPrint());
                selfConfigurationVo.setPrintId(selfConfiguration.getId());
            }
        }

        return AjaxResult.success(selfConfigurationVo);
    }

    @ApiOperation("更新是否自助打印标志")
    @PostMapping("updateIsPrint")
    public AjaxResult updateIsPrint(@RequestBody UsSelfConfiguration usSelfConfiguration) {
        if (usSelfConfiguration.getId() == null) {
            AjaxResult.error("id为null");
        }
        usSelfConfigurationService.updateById(usSelfConfiguration);
        return AjaxResult.success("更新成功");
    }

    @ApiOperation("无脑查询进入方式")
    @GetMapping("getSelfEntryType")
    public AjaxResult getSelfEntryType() {
        LambdaQueryWrapper<UsSelfConfiguration> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UsSelfConfiguration> eq = wrapper.eq(UsSelfConfiguration::getDescribe, "自助机进入方式");
        List<UsSelfConfiguration> usSelfConfigurations = usSelfConfigurationService.list(eq);
        return AjaxResult.success(usSelfConfigurations);
    }


    @ApiOperation("通过自助机唯一标识，修改自助机进入方式")
    @PostMapping("saveSelfEntryType")
    public AjaxResult saveSelfEntryType(@RequestBody UsSelfConfiguration usSelfConfiguration) {
        usSelfConfiguration.setDescribe("自助机进入方式");
        LambdaQueryWrapper<UsSelfConfiguration> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UsSelfConfiguration> eq = wrapper.eq(UsSelfConfiguration::getDescribe, usSelfConfiguration.getDescribe());
        usSelfConfigurationService.update(usSelfConfiguration,eq);
        return AjaxResult.success("操作成功");
    }

    @ApiOperation("修改自助机进入方式")
    @PostMapping("updateSelfEntryType")
    public AjaxResult updateSelfEntryType(@RequestBody UsSelfConfiguration usSelfConfiguration) {
        usSelfConfiguration.setDescribe("自助机进入方式");
        LambdaQueryWrapper<UsSelfConfiguration> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UsSelfConfiguration> eq = wrapper.eq(UsSelfConfiguration::getDescribe,"自助机进入方式");
        usSelfConfigurationService.update(usSelfConfiguration,eq);
        return AjaxResult.success("操作成功");
    }

    @ApiOperation("查询设备列表")
    @GetMapping("getMachineList")
    public AjaxResult getMachineList() {
        List<UsMachineList> usMachineLists = usMachineListService.list();
        return AjaxResult.success(usMachineLists);
    }

    @ApiOperation("查询打印设置")
    @GetMapping("getPrintSetById/{id}")
    public AjaxResult getPrintSetById(@PathVariable("id") Integer id) {

        UsPrintSet printSet = usPrintSetService.getById(id);

        return AjaxResult.success(printSet);
    }

    @ApiOperation("修改打印设置")
    @PostMapping("updatePrintSetById")
    public AjaxResult updatePrintSetById(@RequestBody UsPrintSet usPrintSet) {
        usPrintSetService.updateById(usPrintSet);
        return AjaxResult.success();
    }

    @ApiOperation("获取在线打印机")
    @PostMapping("getPrintMachine")
    public AjaxResult getPrintMachine() {
        ArrayList<String> printMachine = new ArrayList<>();
        for (PrintService ps : PrinterJob.lookupPrintServices()) {
            String psName = ps.toString();
            String psNameResult = psName.replaceFirst("Win32 Printer : ", "");
            printMachine.add(psNameResult);
        }
        return AjaxResult.success(printMachine);
    }

}


